package one.two.eighteen;

public class Accumulator {

	private double m;
	private double s;
	private int N;

	public void addDataValue(double x) {
		N++;
		m = m + (x - m) / N;
		s = s + 1.0 * (N - 1) / N * (x - m) * (x - m);
	}

	public double mean() {
		return m;
	}

	public double var() {
		return s / (N - 1);
	}

	public double stddev() {
		return Math.sqrt(this.var());
	}

	public static void main(String[] args) {
		Accumulator a = new Accumulator();
		for (int i = 0; i < 10000; i++) {
			a.addDataValue(0.00000000001);
		}
		System.out.println(a.mean());

		AccumulatorOriginal b = new AccumulatorOriginal();
		for (int i = 0; i < 10000; i++) {
			b.addDataValue(0.0000001);
		}
		System.out.println(b.mean());
	}

}
